﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using BaseLinQ.DataContex;
using DevExpress.XtraReports.UI;
using Module;
using Solution.AppCode;

namespace Solutions.Admin.Report
{
    public partial class rp_BaoCaoChucNang : DevExpress.XtraReports.UI.XtraReport
    {
        string _conn = ConfigurationSettings.AppSettings["ConnectString"];
        RBACDataContext db = null;
        public rp_BaoCaoChucNang()
        {
            InitializeComponent();

        }
        public void BindData(DataTable data)
        {
            xrt_ngayketxuat.Text = "Ngày " + DateTime.Now.Day + " tháng " + " " + DateTime.Now.Month + " năm " + DateTime.Now.Year;
            db = new RBACDataContext(_conn);
            xrL_ChiNhanh.Text = "Chi nhánh:" + " " + (from m in db.tbl_Donvis
                                                      where m.pk_iDonViID == UtilsData.GetUserDepartment()
                                                      select m.sTenDonVi).FirstOrDefault();
            DataSource = data;
            xrttenmenu.DataBindings.Add("Text", DataSource, "sTenMenu");
            xrttencontrol.DataBindings.Add("Text", DataSource, "sTenControl");
            // xr_donvi.DataBindings.Add("Text", DataSource, "sTenDonVi");
            xrttenthumuc.DataBindings.Add("Text", DataSource, "sTenThuMuc");
            xrtcap.DataBindings.Add("Text", DataSource, "iCap");
            xrtsothutu.DataBindings.Add("Text", DataSource, "sGhiChu");

        }
        int STT = 1;
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrt_stt.Text = STT.ToString();
            STT++;
        }
        public DevExpress.XtraReports.UI.XtraReport CreateReport()
        {
            rp_BaoCaoChucNang baoCaoChucNang = new rp_BaoCaoChucNang();
            MenuLogic menuLogic = new MenuLogic();
            List<tbl_Menu> lstMenu = menuLogic.GetTreeMenu();
            if (lstMenu != null && lstMenu.Count > 0)
            {
                DataTable dt = ConvertToDataTable(lstMenu);
                baoCaoChucNang.BindData(dt);
            }

            return baoCaoChucNang;
        }
        public DataTable ConvertToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties =
               TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;

        }
    }
}
